package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GameAttribute;
import com.ruoyi.system.model.domain.GoodsInfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.model.mapper.InfoImgMapper;
import com.ruoyi.system.model.mapper.SuprAuthMapper;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.OutUrlConstant;
import com.ruoyi.system.outto.core.RegionalCovert;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.goods.NEWLCAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class NEWLCAPIImpl implements NEWLCAPI {

    @Resource
    private InfoImgMapper infoImgMapper;
    @Resource
    private SuprAuthMapper suprAuthMapper;
    @Resource
    private CommonApi commonApi;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private ArtRecMapper artRecMapper;

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(goodsInfoAndAttrValueDto);
        log.info("老曹添加接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(OutUrlConstant.LCADD, JSONObject.toJSONString(params), "application/json");
        log.info("老曹添加接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);
        if (!"200".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
            String data = resultJson.getString("data");
            ArtRec artRecreq = new ArtRec();
            artRecreq.setConfig(salePartner.getId());
            artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
            List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
            if(artRecs.size()>0) {
                artRecs.get(0).setOutid(data);
                artRecMapper.updateArtRec(artRecs.get(0));
            }else{
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                artRecreq.setCreateId(goodsInfoAndAttrValueDto.getCreateId());
                artRecreq.setType(LCConstant.interAdd);
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setSaleCode(salePartner.getSaleName());
                artRecreq.setOutid(data);
                artRecMapper.insertArtRec(artRecreq);
            }
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> params = params(goodsInfoAndAttrValueDto);
        log.info("老曹编辑接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(OutUrlConstant.LCAEDIT, JSONObject.toJSONString(params), "application/json");
        log.info("老曹编辑接口返回参数={}", response);
        JSONObject resultJson = JSONObject.parseObject(response);

        if (!"200".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }


        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }


    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto) {

       String des = goodsInfoAndAttrValueDto.getDes().replace("包人脸", "").replace("营地号", "")
                .replace("人脸", "")
                .replace("脸", "")
                .replace("有二次", "可二次")
                .replace("实名", "")
                .replace("未成年人", "")
                .replace("暑假", "")
                .replace("高中", "")
                .replace("防沉迷", "")
                .replace("身份证", "")
                .replace("实名信息", "")
                .replace("身份证号码", "")
                .replace("实名", "")
                .replace("人脸验证", "")
                .replace("未成年词", "")
                .replace("小学", "")
                .replace("初中", "")
                .replace("高中", "")
                .replace("营地ID", "")
                .replace("Q号", "")
                .replace("Q", "扣")
                .replace("WX", "V")
                .replace("QQ号", "")
                .replace("手机号", "")
                .replace("微信号", "V")
                .replace("初中", "")
                .replace("到删", "")
                .replace("到删除", "")
                .replace("到除", "")
                .replace("小学", "");
        String title = goodsInfoAndAttrValueDto.getTitle().replace("包人脸", "").replace("营地号", "")
                .replace("人脸", "")
                .replace("脸", "")
                .replace("有二次", "可二次")
                .replace("实名", "")
                .replace("未成年人", "")
                .replace("暑假", "")
                .replace("高中", "")
                .replace("防沉迷", "")
                .replace("身份证", "")
                .replace("实名信息", "")
                .replace("身份证号码", "")
                .replace("实名", "")
                .replace("人脸验证", "")
                .replace("未成年词", "")
                .replace("小学", "")
                .replace("初中", "")
                .replace("高中", "")
                .replace("营地ID", "")
                .replace("Q号", "")
                .replace("Q", "扣")
                .replace("WX", "V")
                .replace("QQ号", "")
                .replace("手机号", "")
                .replace("微信号", "V")
                .replace("初中", "")
                .replace("到删", "")
                .replace("到删除", "")
                .replace("到除", "")
                .replace("小学", "");

        GameAttribute gameAttribute = null;
        if(goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("和平精英")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("原神")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("光遇")){//=================================未配置游戏属性
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")){//=================================未配置游戏属性
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
        }else{
            throw new GlobalException("当前游戏暂不支持");
        }
        String replace = goodsInfoAndAttrValueDto.getDes().replace("找回", "")
                .replace("人脸", "")
                .replace("有二次", "可二次")
                .replace("包脸", "")
                ;
        goodsInfoAndAttrValueDto.setDes(replace);

        String cfRe =  "[\"上海大区-东部\",\"广东大区-南部\",\"广西大区-南部\",\"湖南大区-南部\",\"湖北大区-南部\",\"浙江大区-东部\",\"江苏大区-东部\",\"福建大区-东部\",\"江西大区-南部\",\"陕西大区-西部\",\"四川大区-西部\",\"重庆大区-西部\",\"安徽大区-东部\",\"云南大区-西部\",\"北方大区-北部\",\"辽宁大区-北部\",\"黑龙江大区-北部\",\"吉林大区-北部\",\"北京大区-北部\",\"山东大区-北部\",\"山西大区-北部\",\"河南大区-北部\",\"河北大区-北部\",\"南方大区-南部\",\"教育网大区-东部\",\"移动专区-东部\",\"北方大区-北部\",\"高清竞技大区\"]";
        JSONArray cfReStr = JSONObject.parseArray(cfRe);


        Map<String, Object> params = new TreeMap<>();
        params.put("game", goodsInfoAndAttrValueDto.getGameName());
        if (Objects.isNull(RegionalCovert.getLCPareJson(goodsInfoAndAttrValueDto.getGameName()))) {
            throw new GlobalException("51接口不支持此游戏：" + goodsInfoAndAttrValueDto.getGameName());
        }
        if ("王者荣耀,和平精英,枪战王者".contains(goodsInfoAndAttrValueDto.getGameName())) {
            params.put("cate_name", goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName());
        }else if("原神".equals(goodsInfoAndAttrValueDto.getGameName())){
            if(goodsInfoAndAttrValueDto.getGroupName().contains("米哈游")) {
                params.put("cate_name", "米哈游");
            }else {
                params.put("cate_name", "TapTap");
            }
        }else if("穿越火线".equals(goodsInfoAndAttrValueDto.getGameName())){
            if(goodsInfoAndAttrValueDto.getServerName().contains("黑龙江")) {
                params.put("cate_name",goodsInfoAndAttrValueDto.getServerName().substring(0, 3)+"大区-"+goodsInfoAndAttrValueDto.getGroupName().substring(0,2));
            }else{
                params.put("cate_name",goodsInfoAndAttrValueDto.getServerName().substring(0, 2)+"大区-"+goodsInfoAndAttrValueDto.getGroupName().substring(0,2));
            }
        }else{
            String area = goodsInfoAndAttrValueDto.getGroupName().length()>2?goodsInfoAndAttrValueDto.getGroupName().substring(0,2):goodsInfoAndAttrValueDto.getGroupName();
            params.put("cate_name",goodsInfoAndAttrValueDto.getServerName()+"-"+area);
        }

        /**
         * 查询合作伙伴相关信息
         */
        params.put("platform",  this.salePartner.getOther());

        BigDecimal bigDecimal = commonApi.addPrice(goodsInfoAndAttrValueDto.getPrice(),  this.salePartner.getSeleep());
        commonApi.setPrice(bigDecimal,goodsInfoAndAttrValueDto.getId(), this.salePartner);
        params.put("price", bigDecimal.stripTrailingZeros().toPlainString());

        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);

        String imgs = "";
        if (Objects.nonNull(goodsInfoAndAttrValueDto.getTitleImg())){
            imgs = goodsInfoAndAttrValueDto.getTitleImg() + ",";
        }
        if (!CollectionUtils.isEmpty(infoImgs)) {
            imgs = imgs + infoImgs.stream().map(i -> i.getImgUrl()).collect(Collectors.joining(","));
        } else {
            imgs =  imgs + LCConstant.defaultImg;
        }
        params.put("imgs", imgs);
//        String title = goodsInfoAndAttrValueDto.getDes();
//        if (goodsInfoAndAttrValueDto.getDes().length() > 150) {
//            title = goodsInfoAndAttrValueDto.getDes().substring(0, 150);
//        }
//        title = title.replace(LCConstant.bakFace, "包认证");
//        if (Objects.equals(LCConstant.bakFace, gameAttribute.getFieldName())) {
//            params.put("title", "包认证  " + title);
//        } else {
//            params.put("title",gameAttribute.getFieldName() + "  " + title);
//        }



        params.put("title","【"+descTitle+"_"+goodsInfoAndAttrValueDto.getId()+"】"+title);
        params.put("description", "【"+descTitle+"_"+goodsInfoAndAttrValueDto.getId()+"】"+des.replace("+",""));


        String level = gameAttribute.getFieldName();
        if(goodsInfoAndAttrValueDto.getGameName().contains("原神")){
            if("可修改邮箱".contains(level)){
                params.put("is_indulge", 1);
            }
            if("未绑定邮箱".contains(level)){
                params.put("is_indulge", 0);
            }else{
                params.put("is_indulge", 2);
            }
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")){
            if(gameAttribute.getFieldName().contains("有修改人脸机会")){
                params.put("is_indulge", 2);
            }
            if(gameAttribute.getFieldName().contains("无修改人脸机会")){
                params.put("is_indulge", 1);
            }else{
                params.put("is_indulge", 0);
            }
        }else {
            if (Objects.equals("可二次实名", level)) {
                params.put("is_indulge", 2);
            } else if (Objects.equals(LCConstant.bakFace, level)) {
                params.put("is_indulge", 3);
            } else if (Objects.equals("已删实名", level)) {
                params.put("is_indulge", 0);
            }
        }
        params.put("p_number", descTitle + goodsInfoAndAttrValueDto.getId());

//        if(goodsInfoAndAttrValueDto.getCarrierName().contains("Q")) {
//
//        }else {
//            params.put("account_number", goodsInfoAndAttrValueDto.getAccountNumber());
//        }
        params.put("account_number", goodsInfoAndAttrValueDto.getAccountNumber());


        //游戏类型参数
        String pf_ids = "";
        if (Objects.equals("王者荣耀", goodsInfoAndAttrValueDto.getGameName())) {
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF7())) {
                params.put("hero_num", 0);
            } else {
                params.put("hero_num", goodsInfoAndAttrValueDto.getF7());
            }
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF8())) {
                params.put("skin_num", 0);
            } else {
                params.put("skin_num", goodsInfoAndAttrValueDto.getF8());
            }
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF9());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF10());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            String string = "地狱岩魂---10501,无尽征程---10502,寅虎·御盾---10503,天鹅之梦---10602,纯白花嫁---10603,缤纷独角兽---10604,丁香结---10605,青蛇---10606,音你心动（女）---10607,山海·琳琅生---10608,忍·炎影---10701,未来纪元（赵云）---10702,皇家上将---10703,嘻哈天王---10704,白执事---10705,引擎之心---10706,龙胆---10708,百木心枪---10709,淬星耀世---10710,金属风暴---10801,龙骑士---10802,进击墨子号---10803,神迹守卫（墨子）---10804,降魔---10805,女仆咖啡---10901,魅力维加斯---10902,少女阿狸---10904,热情桑巴---10905,时之彼端---10906,紫罗兰之誓---10907,时之奇旅（妲己）---10908,青丘·九尾---10909,摇滚巨星---11001,暗夜贵公子---11002,优雅恋人---11003,白昼王子---11004,水果甜心---11102,蔷薇恋人---11103,杀手不太冷---11104,末日机甲（孙尚香）---11105,沉稳之力---11106,时之恋人（孙尚香）---11107,异界灵契---11108,音你闪耀---11109,电玩小子---11203,星空梦想---11204,狮舞东方---11205,黑桃队长---11206,乒乒小将---11207,寅虎·瑞焰---11208,鲤鱼之梦---11301,蜃楼王---11302,云端筑梦师---11303,奇妙博物学---11304,玄嵩---11305,高山流水---11306,天秀·幻梦---11307,绅士熊喵---11402,天才门将---11403,秘密基地---11404,唤灵魔甲---11405,电玩·爆裂旋风---11406,死亡摇滚---11502,玩趣恶龙---11503,天秀·音浪---11504,爱心护理---11601,暗夜猫娘---11602,致命风华---11603,节奏热浪---11604,迷踪丽影---11605,王者之锤---11702,海滩丽影---11703,超时空战士（钟无艳）---11704,天使之翼---11802,妖精王---11803,归虚梦演（孙膑）---11804,天狼运算者---11805,寅虎·展翼---11806,救世之瞳---11901,化身博士---11902,炼金王---11903,奇幻香踪---11905,白色死神---12001,狰---12002,星夜王子---12003,乐园追猎者---12004,夜都怪侠---12005,红桃皇后---12101,大秦宣太后---12102,重明---12103,白晶晶---12104,幻夜卜梦---12105,圣诞狂欢---12301,天魔缭乱---12302,末日机甲（吕布）---12303,猎兽之王---12304,野性能量---12305,御风骁将---12306,怒海麟威---12307,真爱至上---12402,赤莲之焰---12403,音你心动（男）---12404,云间偶戏---12502,无心---12503,乘风破浪---12602,无限飓风号---12603,朔风刀---12604,匿光决锋者---12605,冰雪圆舞曲---12701,花好人间---12702,游园惊梦---12703,幽恒---12704,女儿国国王---12705,落雪兰心---12706,幽灵船长---12803,烛龙---12805,天狼征服者---12806,黄金武士---12901,穷奇---12902,蓝屏警告---12903,岱宗---12904,铁甲之心---12905,鬼剑武藏---13001,未来纪元（宫本武藏）---13002,万象初新---13004,地狱之眼---13005,霸王丸---13006,千年之狐---13102,凤求凰---13103,敏锐之力---13104,鸣剑·曳影---13106,诗剑行---13107,逐梦之星---13202,暗影游猎---13203,潮玩牛仔---13204,深海之息---13205,山海·玄木吟---13206,妄想实况---13207,魔术师---13302,超时空战士（狄仁杰）---13303,阴阳师---13304,鹰眼统帅---13305,鹰眼护卫---13305,万华元夜---13306,神器·狴犴令---13307,拳王---13401,大发明家---13402,黄金狮子座---13403,星际陆战队（达摩）---13404,沙漠行僧---13405,苍穹之光---13502,海滩派对---13503,职棒王牌---13504,霸王别姬（男）---13505,科学大爆炸---13506,无限倾心（男）---13507,东方不败---13601,海洋之心---13602,倪克斯神谕---13603,神器·明辉仪---13604,暗渊魔法---13702,圣诞老人---13902,功夫老勺---13903,醍醐杖---13904,航海奇遇记---13905,龙腾万里---14001,冰锋战神---14003,武圣---14005,赤影疾锋---14006,百相守梦（关羽）---14007,圣诞恋歌---14102,仲夏夜之梦---14103,逐梦之音---14104,猫影幻舞---14106,遇见胡旋---14108,唤灵魅影---14109,幻阙歌---14110,玩偶对对碰---14201,魔法小厨娘---14202,心灵骇客---14203,如懿---14204,时之奇旅（安琪拉）---14205,追逃游戏（女）---14206,爱与正义---14401,星际陆战队（程咬金）---14402,功夫厨神---14404,活力突击---14405,演武夺筹---14406,无双福将---14407,哥特玫瑰---14601,紫霞仙子---14603,一生所爱---14604,瓷语鉴心---14605,启示之音---14606,时尚教父---14801,炽热元素使---14802,德古拉伯爵---14903,夺宝奇兵---14904,虎啸剑宗---14905,街头霸王---15001,教廷特使---15002,白龙吟---15003,逐梦之影---15004,飞衡---15005,傲雪梅枪---15006,弑枪猎影---15007,偶像歌手---15202,凤凰于飞---15203,幻想奇妙夜---15204,午后时光---15207,星穹之声---15208,暗隐猎兽者---15302,驯魔猎人---15303,默契交锋（男）---15304,金庭之子---15305,兔女郎---15402,水晶猎龙者---15403,青春决赛季---15404,冠军飞将---15405,瑞麟志---15406,默契交锋（女）---15407,九霄神辉---15408,燃星之曲---15409,奇遇舞章---15502,幽兰居士---15603,黄金白羊座---15604,缤纷绘卷---15605,千筹问战---15606,魅语---15701,绯月行---15702,晚萤---16201,前尘镜---16202,修罗---16301,枫霜尽---16302,死亡骑士---16601,狮心王---16602,心灵战警---16603,潮玩骑士王---16604,追逃游戏（男）---16605,地狱火---16701,西部大镖客（孙悟空）---16702,美猴王---16703,至尊宝---16704,全息碎影---16705,大圣娶亲---16706,零号·赤焰---16707,孙行者---16708,齐天大圣---16709,西部大镖客（牛魔）---16801,制霸全明星---16802,御旌---16803,奔雷神使---16804,牛运亨通---16805,阿尔法小队---16902,辉光之辰---16903,黄金射手座---16904,如梦令(男)---16905,圣弓游侠---16906,无尽星芒---16907,万事如意---17001,纽约教父---17002,汉昭烈帝---17003,刘备*时之恋人---17004,潮玩造梦师---17005,百相守梦（刘备）---17006,五福同心---17101,乱世虎臣---17102,虎魄---17104,百相守梦（张飞）---17105,黑猫爱糖果---17302,逐浪之夏---17303,银河之约---17304,飞鸢探春---17305,云中旅人---17306,妄想特派---17308,霸王别姬（女）---17402,凯尔特女王---17403,云霓雀翎---17404,启明星使---17405,无限倾心（女）---17407,神迹守卫（钟馗）---17502,乐园奇幻夜---17503,驱傩正仪---17504,遇见飞天---17602,寅虎·心曲---17604,银翎春语---17605,永曜之星---17802,次元傲视---17803,天秀·启明---17804,潮玩骑兵---17805,朔望之晖---17902,补天---17903,逐梦之翼---18002,次元突破---18003,雪上飞焰---18004,热血海滩---18005,冰霜恋舞曲---18202,久胜战神---18203,胡桃夹子---18204,真爱魔法---18205,画中仙---18206,冰冠公主---18302,单词大作战---18304,黎明之约---18305,奇迹圣诞---18402,舞动绿茵---18403,繁星吟游---18404,花朝如约---18405,电玩·兔顽号---18406,圆桌骑士（太乙真人）---18601,饕餮---18602,华丽摇滚---18603,劲辣红锅---18604,逐梦之光---18702,灼幽烈阳---18703,幻乐之宴---18902,原初探秘者---18903,五谷丰年---18904,黄金分割率---19002,武陵仙君---19003,掌控之力---19004,时雨天司---19005,星域神启---19006,鹤羽星尊---19007,守护之力---19102,猫狗日记（女）---19103,白蛇---19104,白鹤梁神女---19105,挚爱花嫁---19106,烈魂---19203,火炮绅士---19204,曙光守护者---19302,青龙志---19303,绛天战甲---19304,银白咏叹调---19305,坚韧之力---19402,玄武志---19403,千军破阵---19404,白虎志---19502,原初追逐者---19503,热力回旋---19504,特工魅影---19603,朱雀志---19604,碎云---19605,真我赫兹（百里守约）---19606,混沌棋---19702,滕王阁序---19703,胖达荣荣---19802,顽趣（梦奇）---19803,天降福星---19804,蜜橘之夏---19902,无限星赏官---19903,祈雪灵祝---19904,玉兔公主---19905,记忆之芯---19906,鲨炮海盗猫---31202,星空之诺---31203,月团寄思---31204,大漠名商---31205,匿光破解者---31206,虹云星官---50102,疑决卦---50103,吟游魔法---50104,梅西---50202,天狼狩猎者---50203,李小龙---50204,寅虎·赤拳---50205,擒涛扼浪---50206,命运角斗场---50301,御狮---50302,特工战影---50303,电玩高手---50304,御霄---50402,胡桃异想国---50403,契约魔法---50404,完美假期---50405,遇见神鹿---50502,时之祈愿（女）---50503,山海·碧波行---50504,真我赫兹(瑶)---50505,纤云弄巧---50602,时之祈愿（男）---50603,一念神魔---50702,山海·炽霜斩---50703,箭羽风息---50802,太华---50803,天狼溯光者---50804,炽翼辉光---50805,御銮---50902,圆桌骑士（盾山）---50903,梦圆繁星---50904,猫狗日记（男）---51002,末日机甲（孙策）---51003,挚爱之约---51004,西部大镖客（猪八戒）---51102,猪悟能---51103,潮玩探月行---51104,梁祝---51302,天狼绘梦者---51303,神器·万象笔---51304,妄想奇谈---51305,如梦令（女）---51502,拒霜思---51503,暖冬·兔眠---51504,神威---51803,无双飞将---51804,幻泉雾影---52101,浮梦罗烟---52102,归虚梦演（曜）---52201,云鹰飞将---52202,李逍遥---52203,山海·苍雷引---52204,归虚梦演（西施）---52301,诗语江南---52303,游龙清影---52304,玲珑珍味---52305,归虚梦演（蒙犽）---52401,狂想玩偶喵---52402,龙鼓争鸣---52403,胡桃狂想曲---52404,归虚梦演（鲁班大师）---52501,乓乓大师---52502,匿光启智者---52503,探海日志---52504,蔚蓝守将---52702,赏金猎手---52802,电玩·雷克斯---52803,重装意志---52902,冰霜神祇---52903,炽阳神光---53102,匿光追影者---53103,玫瑰异探---53104,真我赫兹（镜）---53105,顽趣（阿古朵）---53302,海盐诗旅---53402,永昼---53601,浮生妄---53602,鹤归松栖（女）---53803,唐三藏---54002,星界游侠---54202,太极少年---54401,鹤归松栖（男）---54402,西部游侠---54501,危途狂花---54801,驭风魔法---54802,零食大作战---56401,乞巧织情---15206001,女武神---15501301";
            String [] strings = string.split(",");
            for(GameAttribute gameAttribute1 : gameAttributesss){
                String fieleName = gameAttribute1.getFieldName();
                for(String s : strings){
                    String[] ss = s.split("---");
                    String pifu = ss[0];
                    if(pifu.contains("（")){
                        String[] pifus = pifu.split("（");
                        pifu = pifus[1].replace("）","")+"-"+pifus[0];
                    }
                    if(fieleName.contains(pifu)){
                        pf_ids += "|"+ss[1];
                    }
                }
            }
            params.put("camp_id", StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF17())?"123456":goodsInfoAndAttrValueDto.getF17());
        } else if (Objects.equals("和平精英", goodsInfoAndAttrValueDto.getGameName())) {
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF1());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF2());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF3());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF4());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF5());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF8())) {
                params.put("arms_num", 0);
            } else {
                params.put("arms_num", goodsInfoAndAttrValueDto.getF8());
            }
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF9())) {
                params.put("skin_num", 0);
            } else {
                params.put("skin_num", goodsInfoAndAttrValueDto.getF9());
            }
            if(gameAttributesss.size()>0) {
                for (GameAttribute gameAttribute1 : gameAttributesss) {
                    pf_ids += "|"+gameAttribute1.getFieldName();
                }
            }
            params.put("camp_id", goodsInfoAndAttrValueDto.getF13()==null||goodsInfoAndAttrValueDto.getF13().equals("")?"123456":goodsInfoAndAttrValueDto.getF13());
        } else if (Objects.equals("英雄联盟", goodsInfoAndAttrValueDto.getGameName())) {
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF7());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF8());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF9());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF10());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF13());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF14());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF16());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF17());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            if(gameAttributesss.size()>0) {
                for (GameAttribute gameAttribute1 : gameAttributesss) {
                    pf_ids += "|"+gameAttribute1.getFieldName();
                }
            }
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF3())) {
                params.put("hero_num", 0);
            } else {
                params.put("hero_num", goodsInfoAndAttrValueDto.getF3());
            }
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF4())) {
                params.put("skin_num", 0);
            } else {
                params.put("skin_num", goodsInfoAndAttrValueDto.getF4());
            }
        } else if(Objects.equals("穿越火线",goodsInfoAndAttrValueDto.getGameName())){
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF8())) {
                params.put("hero_arms", 0);
            } else {
                params.put("hero_arms", goodsInfoAndAttrValueDto.getF8());
            }
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF13());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF14());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF15());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF16());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF17());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF18());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF19());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF20());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            if(gameAttributesss.size()>0) {
                for (GameAttribute gameAttribute1 : gameAttributesss) {
                    pf_ids += "|"+gameAttribute1.getFieldName();
                }
            }
        }else if(Objects.equals("枪战王者",goodsInfoAndAttrValueDto.getGameName())){
            if(StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF2())){
                throw new GlobalException("实名不能为空");
            }
            GameAttribute gameAttribute1 = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF2()));
            if (Objects.isNull(gameAttribute1.getFieldName())) {
                params.put("vip_num", "无");
            } else {
                params.put("vip_num", gameAttribute1.getFieldName());
            }
            if (StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF5())) {
                params.put("hero_arms", 0);
            } else {
                params.put("hero_arms", goodsInfoAndAttrValueDto.getF5());
            }
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF7());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF8());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF12());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF13());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            if(gameAttributesss.size()>0) {
                for (GameAttribute gameAttribute2 : gameAttributesss) {
                    pf_ids +="|"+gameAttribute2.getFieldName();
                }
            }
        }else if(Objects.equals("原神",goodsInfoAndAttrValueDto.getGameName())){
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF11())) {
                params.put("role_num", "无");
            } else {
                params.put("role_num", goodsInfoAndAttrValueDto.getF11().split(",").length);
            }
            if (Objects.isNull(goodsInfoAndAttrValueDto.getF17())) {
                params.put("arms_num", "无");
            } else {
                params.put("arms_num", goodsInfoAndAttrValueDto.getF17().split(",").length);
            }
            String pfIds = "";
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF11());
            pfIds = reIds(pfIds, goodsInfoAndAttrValueDto.getF17());
            List<GameAttribute> gameAttributesss = getGameAttributs(pfIds);
            if(gameAttributesss.size()>0) {
                for (GameAttribute gameAttribute2 : gameAttributesss) {
                    pf_ids += "|"+gameAttribute2.getFieldName();
                }
            }
        }
        if (pf_ids.length()>2) {
            params.put("pf_str", "("+pf_ids.substring(1).trim() + ")");
        }
        String cover = MapUntils.getCover(params);
        cover = cover + "key=" +  this.salePartner.getSaleKey();
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        log.info("sign={}", sign);
        params.put("sign", sign);
        return params;
    }

    private List<GameAttribute> getGameAttributs(String ids) {
        return gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                Long.class));
    }
    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner sa) {
        Map<String, Object> params = new TreeMap<>();
        params.put("p_number", descTitle + goodsInfoAndAttrValueDto.getId());
        params.put("platform", sa.getOther());

        //sign
        String cover = MapUntils.getCover(params);
        cover = cover + "key=" + sa.getSaleKey();
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
        log.info("老曹删除接口请求参数={}", JSONObject.toJSONString(params));
        String response = HttpUtils.sendPost(OutUrlConstant.LCADEL, JSONObject.toJSONString(params), "application/json");
        if (StringUtils.isEmpty(response)) {
            throw new GlobalException("请求接口失败");
        }
        log.info("老曹删除接口返回参数={}", JSONObject.toJSONString(response));
        JSONObject resultJson = JSONObject.parseObject(response);
        Map<String, Object> result = new HashMap<>();

        if (!"200".equals(resultJson.getString("code"))) {
            result.put("code", 500);
        } else {
            result.put("code", 200);
        }
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(sa.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        if(artRecs.size()>0) {
            artRecMapper.deleteArtRecById(artRecs.get(0).getId());
        }

        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        result.put("msg", resultJson.getString("data"));
        return AjaxResult.success(result);
    }



    private String getRea(){
        Map<String, Object> params = new TreeMap<>();
        params.put("platform","xtdanbao");

        String cover = MapUntils.getCover(params);
        cover = cover + "key=wvvbt1rjwvms7o7e319jm7p28jhbqdc3";
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
//        String response = HttpUtils.sendPost(OutUrlConstant.LCADD, JSONObject.toJSONString(params),"application/json");
        String response = HttpUtils.sendPost("https://www.51youxihao.com/index.php?g=api&m=product&a=list_games", JSONObject.toJSONString(params), "application/json");
        System.out.println(response);
        return response;
    }

    public static void main(String[] args) {

//        String aa = "王者荣耀";
//        String substring = aa.substring(0, 2);
//        System.out.println(substring);
//
//        {
//            "game": "王者荣耀",
//                "price": "1800",
//                "cate_name": "安卓QQ",
//                "p_number": "252309",
//                "imgs":"https://youxihao.oss-cn-zhangjiakou.aliyuncs.com/5f7e912c7f7a7.jpg,https://youxihao.oss-cn-zhangjiakou.aliyuncs.com/5f7e912cb88a8.jpg,https://youxihao.oss-cn-zhangjiakou.aliyuncs.com/5f7e912cec9fc.jpg",
//                "description": "4S 11A 三星长门 三星秽土猿飞日斩 三星金角银角 三星鬼灯幻月 67个秽土波风水门碎片 52个猿飞日斩碎片",
//                "platform": "51youxihao",
//                "title": "WT【安卓抠】50英雄31皮肤安卓Q-50英雄31皮肤-夺萃鹰眼祸源150级，至尊宝，忍炎影，龙腾万里，死亡",
//                "is_indulge": 1,
//                "sign": "DBA10184D15A13385E29BE5DCD461ABC",
//        }
//

//        String str = "gm28-安卓q-V7-108英雄-185皮肤-300五级铭文 7王者印记 未来机甲 幽冥火 253幸运值 " +
//                "1内测 蔷薇恋人 零号赤焰 天狼绘梦者 天狼运算者 天狼溯光者 祈雪灵祝 逐梦之星 朱雀志 异界灵契 拒霜思 " +
//                "孙行者 猪悟能 梁祝 地狱火 久胜战神 云霓雀翎 末日机甲女 无限星赏官 原初追逐者 黎明之约 原初探秘者 李小龙 " +
//                "暗隐猎兽者 金属风暴 热力回旋 特工魅影 逐浪之夏 修罗 白龙吟 千年之狐 街头霸王 箭羽风息 星空之诺 鬼剑武藏 " +
//                "朔望之晖 德古拉伯爵 银河之约 暗影游猎 云间偶戏 死神来了 冰冠公主 狂想玩偶喵 单词大作战 花好人间 狰 饕餮 " +
//                "烛龙 重明 穷奇 御霄 御旌 御銮 朔风刀 混沌棋 疑决卦 醍醐杖 万华元夜 女仆咖啡 科学大爆炸 重装意志 奇遇舞章 " +
//                "永昼 营地ID:459926613";
//        String substring = str.substring(0, 150);
//        System.out.println(substring);


        Map<String, Object> params = new TreeMap<>();
//        params.put("game", "王者荣耀");
//        params.put("price", new BigDecimal(1800));
//        params.put("cate_name", "安卓QQ");
//        params.put("p_number", 14356);
//        params.put("imgs", "https://asdasda");
//        params.put("description", "1fasdfadsfasd1");
//        params.put("platform", "yud");
//        params.put("title", "222fdsafasd");

        params.put("platform","xtdanbao");
//        params.put("key","wvvbt1rjwvms7o7e319jm7p28jhbqdc3");

        String cover = MapUntils.getCover(params);
        cover = cover + "key=wvvbt1rjwvms7o7e319jm7p28jhbqdc3";
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
//        String response = HttpUtils.sendPost(OutUrlConstant.LCADD, JSONObject.toJSONString(params),"application/json");
        String response = HttpUtils.sendPost("https://www.51youxihao.com/index.php?g=api&m=product&a=list_games", JSONObject.toJSONString(params), "application/json");
        System.out.println(response);
    }

}
